﻿Imports System.IO
Imports System.Net
Imports System
Imports System.Threading
Imports System.Text
Imports System.Net.HttpWebRequest




Public Class frmMain
    Private WithEvents Classy As EventsFunc
    Private WithEvents Typer As TypingThreadder
    Private Property UserAgent As String
    Shared IsConnected As Boolean
    Shared StrangerID As String
    Shared IDParsed As String
    Shared cooKies As System.Net.CookieContainer
    Shared AppRun As Boolean
    Shared SoftConnected As Boolean
    Shared StrangerTyping As Boolean
    Shared YouTyping As Boolean
    Shared DLMessages() As String
    Shared Noobmsg As String
    Shared LastMessage As String
    Shared YouTypingCode As Boolean
    Shared TypeSent As Boolean
    Dim DoneDisCon As Integer

    'Dim Thr As New Thread(New ThreadStart(AddressOf Classy.CheckEventsF))
    Dim Thr
    'Dim TypThr
    Dim TypThr

    Class EventsFunc
        Public Event GM(ByVal GMSG As String)
        Public MessageG As String
        Public Sub CheckEventsF()

            Application.DoEvents()
            'MsgBox("noob")
            '

            'MsgBox(frmMain.IsConnected)
            While frmMain.IsConnected = True
                If frmMain.IsConnected = True Then
                    'MsgBox("noob2")
                    Dim URL As String = "http://promenade.omegle.com/events"
                    Dim request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
                    request.CookieContainer = frmMain.cooKies
                    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.127 Safari/534.16"
                    request.Method = "POST"

                    Dim PostData As String
                    PostData = "id=" & frmMain.IDParsed
                    'PostData = ""
                    Dim ByteArray As Byte() = Encoding.UTF8.GetBytes(PostData)
                    request.ContentType = "application/x-www-form-urlencoded"
                    request.ContentLength = ByteArray.Length
                    Dim dataStream As Stream = request.GetRequestStream()
                    dataStream.Write(ByteArray, 0, ByteArray.Length)
                    dataStream.Close()
                    Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)


                    Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
                    Dim str As String = reader.ReadLine()

                    If str <> "null" Then
                        'Debug.Print(str)
                        Dim SecNum As Integer = 0
                        If InStr(str, "], [") <> 0 Then
                            Application.DoEvents()
                            Do While InStr(str, "], [") <> 0
                                'MsgBox("loopan")
                                Dim NewStr As String
                                SecNum += 1
                                'Debug.Print("SecNum: " & SecNum)
                                ReDim Preserve frmMain.DLMessages(SecNum)

                                If SecNum = 1 Then
                                    'Debug.Print("StorringToarray: " & Mid(str, 2, InStr(str, "], [") - 1))
                                    frmMain.DLMessages(SecNum) = Mid(str, 2, InStr(str, "], [") - 1)
                                Else
                                    'debug.Print("StorringToarray: " & Mid(str, 1, InStr(str, "], [") - 1))
                                    frmMain.DLMessages(SecNum) = Mid(str, 1, InStr(str, "], [") - 1)
                                End If

                                NewStr = Mid(str, InStr(str, "], [") + 3)
                                If InStr(NewStr, "]]") <> 0 Then
                                    NewStr = Mid(NewStr, 1, Len(NewStr) - 1)
                                Else
                                    NewStr = Mid(NewStr, 1, Len(NewStr))
                                End If

                                'Debug.Print("Newstring: " & NewStr)
                                str = NewStr
                                If InStr(str, "], [") = 0 Then
                                    SecNum += 1
                                    'Debug.Print("storring to array: " & str)
                                    ReDim Preserve frmMain.DLMessages(SecNum)
                                    frmMain.DLMessages(SecNum) = str
                                End If
                            Loop
                        Else
                            ReDim Preserve frmMain.DLMessages(1)
                            str = Mid(str, 2, Len(str) - 1)
                            frmMain.DLMessages(1) = str
                            SecNum = 1



                        End If

                        Dim i As Integer
                        Application.DoEvents()
                        For i = 1 To SecNum
                            'Debug.Print(DLMessages(i))
                            If Mid(frmMain.DLMessages(i), 3, 9) = "connected" Then
                                frmMain.SoftConnected = True
                                'Debug.Print("-Connected")
                            End If
                            If Mid(frmMain.DLMessages(i), 3, 6) = "typing" Then
                                frmMain.StrangerTyping = True
                                'Debug.Print("-StrangerTyping")

                            End If
                            If Mid(frmMain.DLMessages(i), 3, 13) = "stoppedTyping" Then
                                frmMain.StrangerTyping = False
                                'Debug.Print("-StrangerStoppedTyping")
                            End If
                            If Mid(frmMain.DLMessages(i), 3, 10) = "gotMessage" Then
                                StrangerTyping = False
                                'Debug.Print(DLMessages(i))
                                Dim WIPMSG As String
                                WIPMSG = Mid(frmMain.DLMessages(i), 17)
                                'Debug.Print("StrangerMessage:" & Mid(DLMessages(i), 17, Len(DLMessages(i)) - 6))
                                Dim TRM As Integer
                                TRM = Len(WIPMSG) - 3

                                Debug.Print(WIPMSG.Remove(TRM))
                                'AddToText("Stranger: " & WIPMSG.Remove(TRM))
                                'frmMain.Noobmsg = WIPMSG.Remove(TRM)
                                Dim MsgGot As String
                                MsgGot = WIPMSG.Remove(TRM)
                                MessageG = MsgGot
                                RaiseEvent GM(MsgGot)

                            End If

                            If InStr(frmMain.DLMessages(i), "strangerDisconnected") <> 0 Then
                                'Debug.Print("-StrangerDC'd")
                                frmMain.IsConnected = False
                                frmMain.btnSend.Enabled = False
                                frmMain.btnStartChat.Text = "Start Chat"
                            End If
                        Next i


                        'Debug.Print(str)


                    End If
                End If

            End While
            'Me.Close()

        End Sub
        'Private Sub notifymsg(ByVal Mesage As String)
        '    RaiseEvent GM(Mesage)
        'End Sub
    End Class




    Private Function ParseID(ByVal StrangerIN As String) As String
        ParseID = Mid(StrangerIN, 2, 6)

    End Function


    Private Function StartChat() As String
        Dim URL As String = "http://promenade.omegle.com/start"
        Dim request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
        request.CookieContainer = cooKies
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.127 Safari/534.16"
        request.Method = "POST"

        Dim PostData As String
        'PostData = "id=" & IDParsed
        PostData = ""
        Dim ByteArray As Byte() = Encoding.UTF8.GetBytes(PostData)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = ByteArray.Length
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        'derp

        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim str As String = reader.ReadLine()
        If str <> "null" Then
            ' MsgBox(str)
            'Debug.Print("Startchat: " & str)
        End If
        StartChat = str

    End Function
    Private ReadOnly mWaitForThread As New AutoResetEvent(False)

    Private Sub btnStartChat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartChat.Click
        Me.txtInput.Enabled = True
        DoneDisCon = 0
        If btnStartChat.Text = "Start Chat" Then
            Thr = Nothing
            StrangerID = StartChat()
            'MsgBox(StrangerID)
            'IsConnected = True
            AppRun = True
            IDParsed = ParseID(StrangerID)
            'MsgBox(IDParsed)
            If IDParsed <> "" Then
                IsConnected = True
                btnSend.Enabled = True
                Thr = New Thread(New ThreadStart(AddressOf Classy.CheckEventsF))
                Thr.Start()
                btnStartChat.Text = "Disconnect"
                AddToText("Stranger " & IDParsed & " Connected")
            Else
                MsgBox("Connection Failure")
            End If
        ElseIf btnStartChat.Text = "Disconnect" Then
            btnStartChat.Text = "Sure?"
        ElseIf btnStartChat.Text = "Sure?" Then
            Discon()
            btnSend.Enabled = False
            btnStartChat.Text = "Start Chat"
        End If



    End Sub

    Private Sub Discon()
        If IsConnected = True Then
            Dim URL As String = "http://promenade.omegle.com/disconnect"
            Dim request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
            request.CookieContainer = cooKies
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.127 Safari/534.16"
            request.Method = "POST"

            Dim PostData As String
            PostData = "id=" & IDParsed
            'PostData = ""
            Dim ByteArray As Byte() = Encoding.UTF8.GetBytes(PostData)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = ByteArray.Length
            Dim dataStream As Stream = request.GetRequestStream()
            dataStream.Write(ByteArray, 0, ByteArray.Length)
            dataStream.Close()
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)


            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
            Dim str As String = reader.ReadLine()
            If str <> "null" Then
                ' MsgBox(str)
                'Debug.Print("EndChat: " & str)
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Discon()
        'IsConnected = False
    End Sub

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Me.CheckEventsT.Abort()
        Discon()
        btnSend.Enabled = False
        btnStartChat.Text = "Start Chat"
        If Thr Is Nothing Then

        Else

            If Thr.ThreadState = ThreadState.Running Then
                Thr.Abort()
                Thr = Nothing



            End If
        End If
        TypThr.Abort()


    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtChat.BackColor = SystemColors.Window
        Classy = New EventsFunc
        Typer = New TypingThreadder

        TypThr = New Thread(New ThreadStart(AddressOf Typer.TypingIT))
        TypThr.Start()
        b4suspened()

    End Sub
    Private Sub AddToText(ByVal AddingTX As String)
        If Me.txtChat.Text <> vbNullString Then
            Me.txtChat.Text = txtChat.Text & vbNewLine & AddingTX
        Else
            Me.txtChat.Text = AddingTX
        End If
        txtChat.SelectionStart = txtChat.TextLength
        txtChat.ScrollToCaret()
    End Sub

    Private Sub tmrconnected_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrconnected.Tick
        If Typer.Typing = Typer.AreWeTyping Then
            'b4suspened()
        ElseIf TypThr.threadstate = 0 Then
            'TypThr.resume()
        End If
        If Classy.MessageG <> LastMessage Then
            AddToText("Stranger: " & Classy.MessageG)
            LastMessage = Classy.MessageG
            'txtChat.SelectionStart = Len(txtChat)
            txtChat.SelectionStart = txtChat.TextLength
            txtChat.ScrollToCaret()
        End If
        If DoneDisCon = 0 Then
            If IsConnected = False Then
                Me.txtInput.Enabled = False
                Me.txtInput.Text = vbNullString
                Me.btnSend.Enabled = False
                Me.btnStartChat.Text = "Start Chat"
                If Thr Is Nothing Then

                Else

                    If Thr.ThreadState = ThreadState.Running Then
                        Thr.Abort()
                        Thr = Nothing



                    End If
                End If
                DoneDisCon = 1
            End If

        End If
        If StrangerTyping = True Then
            CheckBox1.Checked = True
            txtTyping.Text = "Stranger Is Typing..."
        Else
            CheckBox1.Checked = False
            txtTyping.Text = ""
        End If
        CheckBox2.Checked = YouTypingCode
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If IsConnected = True Then
            If SendMsg(Me.txtInput.Text) = True Then
                AddToText("You: " & txtInput.Text)
                txtInput.Text = vbNullString
                Typer.Typing = False
                b4suspened()
                YouTypingCode = False
            End If

        End If
    End Sub
    Private Function SendMsg(ByVal Message As String) As Boolean
        If IsConnected = True Then
            Dim URL As String = "http://promenade.omegle.com/send"
            Dim request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
            request.CookieContainer = cooKies
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.127 Safari/534.16"
            request.Method = "POST"

            Dim PostData As String
            PostData = "id=" & IDParsed & "&msg=" & Message
            'PostData = ""
            Dim ByteArray As Byte() = Encoding.UTF8.GetBytes(PostData)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = ByteArray.Length
            Dim dataStream As Stream = request.GetRequestStream()
            dataStream.Write(ByteArray, 0, ByteArray.Length)
            dataStream.Close()
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)


            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
            Dim str As String = reader.ReadLine()
            If str <> "null" Then
                ' MsgBox(str)
                'Debug.Print("SendMsg: " & str)
                If str = "win" Then
                    SendMsg = True
                    txtChat.SelectionStart = txtChat.TextLength
                    txtChat.ScrollToCaret()
                Else
                    SendMsg = False
                End If
            Else
                SendMsg = False

            End If
        Else
            SendMsg = False
        End If


    End Function

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(Noobmsg)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        IsConnected = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MsgBox(Classy.MessageG)
    End Sub

    Private Sub txtInput_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyDown
        'Debug.Print(e.KeyCode)
        If e.KeyCode = Keys.Enter Then
            If IsConnected = True Then
                If SendMsg(Me.txtInput.Text) = True Then
                    AddToText("You: " & txtInput.Text)
                    txtInput.Text = vbNullString
                    b4suspened()

                End If

            End If
        End If

        If e.KeyCode <> Keys.Enter Then
            If YouTypingCode = False Then
                If TypThr.threadstate = 64 Then TypThr.Resume()
                Typer.Typing = True
                tmrTyping.Dispose()
                tmrTyping.Interval = "1500"
                tmrTyping.Start()
                YouTypingCode = True






            Else
                tmrTyping.Dispose()
                tmrTyping.Interval = "1500"
                tmrTyping.Start()
            End If


        End If



    End Sub

    Private Sub txtInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        MsgBox(txtInput.Text)
        MsgBox(Asc(txtInput.Text))

    End Sub


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        'Typing()
    End Sub

    Private Sub txtTyping_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTyping.TextChanged
        'Debug.Print("changed")
    End Sub

    Private Sub tmrTyping_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTyping.Tick
        If YouTypingCode = True Then
            Typer.Typing = False
            b4suspened()
            tmrTyping.Enabled = False
            YouTypingCode = False
            tmrTyping.Stop()
            tmrTyping.Dispose()
        End If

    End Sub


    Class TypingThreadder
        Public Iscon As String
        Public ID As String
        Public Typing As Boolean
        Public KC As CookieContainer
        Public AreWeTyping As Boolean
        Public Sub TypingIT() 'As Boolean
            ' MsgBox("nub")
            AreWeTyping = False
start:


            If Typing <> AreWeTyping Then
                'MsgBox(Typing)
                'MsgBox(AreWeTyping)
                If frmMain.IsConnected = True Then
retry:
                    'Debug.Print("retyrnan")

                    Dim URL As String = "http://promenade.omegle.com/typing"
                    Dim request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
                    request.CookieContainer = frmMain.cooKies
                    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.127 Safari/534.16"
                    request.Method = "POST"
                    Debug.Print("derp")
                    Dim PostData As String
                    PostData = "id=" & frmMain.IDParsed
                    'PostData = ""
                    Dim ByteArray As Byte() = Encoding.UTF8.GetBytes(PostData)
                    request.ContentType = "application/x-www-form-urlencoded"
                    request.ContentLength = ByteArray.Length
                    Dim dataStream As Stream = request.GetRequestStream()
                    dataStream.Write(ByteArray, 0, ByteArray.Length)
                    dataStream.Close()
                    Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)


                    Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
                    Dim str As String = reader.ReadLine()
                    If str <> "null" Then
                        ' MsgBox(str)
                        Debug.Print("Typing: " & str)
                    End If
                    If str = "win" Then

                        AreWeTyping = Typing

                        Debug.Print("Arewetyping:" & AreWeTyping)
                        Debug.Print("Typing:" & Typing)

                        AreWeTyping = Typing
                        'Thread.Sleep(500)
                    Else
                        GoTo retry
                    End If
                Else
                End If


            End If
            'Debug.Print("start going to")
            GoTo start
        End Sub
    End Class

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TypThr.start()

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        b4suspened()
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        TypThr.Resume()
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        MsgBox(TypThr.ThreadState)
    End Sub

    Private Sub b4suspened()
        tmrSuspened.Interval = 500
        tmrSuspened.Start()

    End Sub

    Private Sub tmrSuspened_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSuspened.Tick
        If tmrTyping.Enabled = False Then
            TypThr.suspend()
            tmrSuspened.Dispose()
        End If


    End Sub
End Class

